//  Copyright (c) Microsoft Corporation.  All Rights Reserved.

using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography.X509Certificates;

using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
using System.ServiceModel.Security.Tokens;

using Common;

namespace Service
{
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);

    }

    class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            Console.WriteLine("Add method is called.");
            double result = n1 + n2;
            return result;
        }

        public double Subtract(double n1, double n2)
        {
            Console.WriteLine("Subtract method is called.");
            double result = n1 - n2;
            return result;
        }

        public double Multiply(double n1, double n2)
        {
            Console.WriteLine("Multiply method is called.");
            double result = n1 * n2;
            return result;
        }

        public double Divide(double n1, double n2)
        {
            Console.WriteLine("Divide method is called.");
            double result = n1 / n2;
            return result;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService));

            serviceHost.Description.Behaviors.Remove<ServiceCredentials>();
            CustomServiceCredentials serviceCredentials = new CustomServiceCredentials();
            // WARNING: The below two settings are not secure options. The sample uses these
            // as the certificates used by this sample are created by makecert which does not
            // have a valid chain trust and is issued by a test root. Production code should
            // not use these settings.
            serviceCredentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "WCFService");
            serviceCredentials.IssuedTokenAuthentication.KnownCertificates.Add(Utils.GetCertificate(StoreName.My, StoreLocation.LocalMachine, X509FindType.FindBySubjectName, "ServiceSTS"));
            serviceCredentials.IssuedTokenAuthentication.CertificateValidationMode = X509CertificateValidationMode.None;
            serviceHost.Description.Behaviors.Add(serviceCredentials);
            serviceHost.Open();

            Console.WriteLine("Press [Enter] to terminate the service.");
            Console.ReadLine();

            serviceHost.Close();
        }
    }
}
